package com.bytecollege.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytecollege.mapper.ArticleMapper;
import com.bytecollege.model.Article;
import com.bytecollege.service.ArticleService;
import com.bytecollege.vo.ArticleDetailVo;
import com.bytecollege.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: ArticleServiceImpl
 * @Description:
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 讨论区模块主页展示
     * @param page
     * @param isType
     * @param value
     * @return
     */
    public IPage<ArticleVo> getAllArticle(Page<ArticleVo> page,
                                           boolean isType,
                                           Object value) {
        QueryWrapper<ArticleVo> articleVoQueryWrapper = new QueryWrapper<>();
        articleVoQueryWrapper.eq("ar.article_state",1)
                .eq(isType, "ar.atype_id", value);
        return articleMapper.getAllArticles(page,articleVoQueryWrapper);
    }

    /**
     * 发表文章
     * @param article
     * @return
     */
    public int addArticle(Article article) {return articleMapper.insert(article);}

    /**
     * 用户通过文章编号删除文章
     * @param articleId
     * @return
     */
    public int delArticle(Integer articleId){return articleMapper.deleteById(articleId);}

    /**
     * 通过文章编号查找已经通过审核的文章
     * @param articleId
     * @return
     */
    public ArticleDetailVo getArticleById(Integer articleId){
        QueryWrapper<ArticleDetailVo> articleDetailVoQueryWrapper = new QueryWrapper<>();
        articleDetailVoQueryWrapper.eq("ar.article_id",articleId);
        articleDetailVoQueryWrapper.eq("ar.article_state",1);
        return articleMapper.getArticleById(articleDetailVoQueryWrapper);
    }

    /**
     * 用户更新文章信息
     * @param article
     * @return
     */
    public int modArticle(Article article){
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Article::getArticleId,article.getArticleId())
                .set(Article::getArticleTitle,article.getArticleTitle())
                .set(Article::getArticleContent,article.getArticleContent())
                .set(Article::getAtypeId,article.getAtypeId())
                .set(Article::getUserId,article.getUserId())
                .set(Article::getArticleClicks,article.getArticleClicks())
                .set(Article::getArticleState,article.getArticleState());
        return articleMapper.update(null,wrapper);
    }

    /**
     * 按照评论数排名
     * @return
     */
    public List<ArticleVo> getHotArticle() {
        return articleMapper.getHotArticle();
    }

    /**
     * 按照阅读量数排名
     * @return
     */
    public List<ArticleVo> getReadArticle() {
        return articleMapper.getReadArticle();
    }

    /**
     * 获取用户主页对应的帖子列表
     * @param page
     * @param userId
     * @return
     */
    public List<ArticleVo> getAllArticle(Page<ArticleVo> page, Integer userId) {
        return articleMapper.getAllArticle(page,userId);
    }
}